package com.ease.concurrent.thread.state;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;

/**
 * @author: chenhs
 * @date: Created in 10:13 2019/12/27
 **/
public class ThreadStateDemo5_IO {

    public static void main(String[] args) throws Throwable {

        Charset charset = Charset.forName("UTF-8");

        Thread t1 = new Thread(() -> {

            try (ServerSocket ss = new ServerSocket(9000)) {

                while (true) {
                    try {
                        System.out.println(" t1即将接收连接...");
                        Socket s = ss.accept();
                        System.out.println(" t1接收到连接...");
                        BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));

                        String mess = null;
                        System.out.println(" t1将接收连接的数据...");
                        while ((mess = reader.readLine()) != null) {
                            System.out.println(mess);
                        }
                        s.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        });


        t1.start();
        Thread.sleep(3000L);
        System.out.println("t1的状态：" + t1.getState());
        Thread.sleep(20000L);
        System.out.println("t1的状态：" + t1.getState());

    }


}
